﻿using System;
using System.Windows.Forms;
using Relocation.Data;
using Relocation.Classes;
using Relocation.Base;
using Relocation.Com;
using Relocation.Forms.ListBase;

namespace Relocation
{
	public partial class huiqian : RelocateesListBase, IPrintForm
	{
		public huiqian()
			: base()
		{
			Initialize();
		}

		public huiqian(Session session)
			: base(session)
		{
			Initialize();
		}

		private void Initialize()
		{
			InitializeComponent();
			this.InitializeRole();
			this.InitializeSearch();
			this.relocateDataview.init();
			this.ObjectQuery = this.Session.Project.Relocatees.CreateSourceQuery();
			this.SortField = Relocatees.GetPropName(t => t.updated);

			
			//this.Refresh();
		}

		/// <summary>
		/// 权限初始化
		/// </summary>
		private void InitializeRole()
		{
			ControlTag roleTag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN,Session.KEY_ROLE_OPERATOR));
			this.ButtonAdd.Tag = roleTag;
			this.ButtonDel.Tag = roleTag;
			this.ButtonEdit.Tag = roleTag;
		}

		/// <summary>
		/// 初始化搜索
		/// </summary>
		private void InitializeSearch()
		{
			this.Field_S_Code.Tag = new ControlTag(null,null, new SearchInfo(Relocatees.GetPropName(t=>t.relocateCode)));
			this.Field_S_Head.Tag = new ControlTag(null, null, new SearchInfo(Relocatees.GetPropName(t => t.head)));
			
			SearchInfo info=new SearchInfo(Relocatees.GetPropName(t=>t.statusFlag),SearchInfo.SearchFieldTypes.Number,new SearchInfo.RanderHandler(delegate(Control control){
				ComboBox comboBox=control as ComboBox;				
				string value=comboBox.Text;
				if(string.IsNullOrEmpty(value))
					return string.Empty;
				return "已安置".Equals(value)?"1":("未安置".Equals(value)?"0":"2");				
			}));
			this.Field_S_Status.Tag = new ControlTag(null,null,info);

		}

		//页面刷新
		public override void Refresh()
		{
			try
			{
				this.Reload();
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
			}
		}

		//新增
		private void add_Click(object sender, EventArgs e)
		{
			try
			{
				using (RelocateWindow rWin = new RelocateWindow(Session))
				{
					rWin.Text = "拆迁户登记";
					rWin.MaximumSize = new System.Drawing.Size(658, 660);
					rWin.MinimumSize = new System.Drawing.Size(658, 660);
					if (rWin.ShowDialog().IsOK())
						this.Refresh();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error("操作失败！");
			}
		}

		//修改
		private void alter_Click(object sender, EventArgs e)
		{
			try
			{
				Relocatees relocate = this.GetSelectEntity();
				if (relocate == null)
					return;

				using (RelocateWindow relocateWindow = new RelocateWindow(Session, relocate))
				{
					relocateWindow.Text = "拆迁户修改";
					relocateWindow.MaximumSize = new System.Drawing.Size(658, 660);
					relocateWindow.MinimumSize = new System.Drawing.Size(658, 660);
					if (relocateWindow.ShowDialog().IsOK())
						this.Refresh();
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		//删除
		private void delete_Click(object sender, EventArgs e)
		{
			try
			{
				Relocatees del = this.GetSelectEntity();
				if (del == null)
					return;
				TreeNode node = this.Session.GetMainTreeView().GetNode(del);
				this.Session.GetMainTreeView().deleteRelocatees(node);
				this.Refresh();
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		//单元格字体设置
		private void relocateDataview_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{

			if (this.relocateDataview.Columns[e.ColumnIndex].DataPropertyName.Equals("statusFlag"))
			{
				int status = -1;
				if (e.Value == null || !int.TryParse(e.Value.ToString(), out status))
				{
					e.Value = "无需安置";
					return;
				}
				if (status == 0)
				{
					e.Value = "未安置";
					e.CellStyle.ForeColor = System.Drawing.Color.Red;
				} else if (status == 1)
				{
					e.Value = "已安置";
					e.CellStyle.ForeColor = System.Drawing.Color.Green;
				} else
				{
					e.Value = "无需安置";
				}
				e.FormattingApplied = true;
			}

		}	

		#region IPrintForm 成员

		void IPrintForm.Print()
		{
			GridToExcel.print(this.relocateDataview);
		}

		#endregion
	}
}
